var express = require('express');
var router = express.Router();

const path = require('path');
const fs = require('fs');

/* GET users listing. */
// router.get('/', function(req, res, next) {
//   res.send('respond with a resource');
// });
let dir = path.join(__dirname, '../persons.json');
let data = fs.readFileSync(dir, 'utf-8');
data = JSON.parse(data);

// 渲染登录页面
router.get('/login', (req, res, next) => {
  res.render('login');
})


// login 提交
router.post('/login', (req, res, next) => {
  // 获取参数
  let { tel, pass } = req.body;
  // 输入框不能为空
  if (!tel || !pass) {
    next('输入框不能为空');
    return;
  }
  // 判断用户是否匹配
  let islogin = data.find(value => value.username == tel && value.password == pass);

  if (islogin) {
    res.render('err',{msg:'登陆成功'})
  } else {
    next('用户名或密码输入错误')
  }
})
// 


module.exports = router;
